<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport( 'joomla.application.component.controller' );
	
	class SocialRoutesController extends JController
	{
		function display()
		{
			$user =& JFactory::getUser();
			if($user->id)
			{
			    //Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = JRequest::getVar('view', 'creation');
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function creationoffroad()
		{
			$user =& JFactory::getUser();
			if($user->id)
			{
			    //Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = JRequest::getVar('view', 'creationoffroad');
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		
		
		function showRoute(){
			
		
			
			$user =& JFactory::getUser();
			if($user->id)
			{
				$id=JRequest::getVar('id', 0);
			    //Read the route in the request
				$modelRoute = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$modelRoute->readRoute($id);
				//Read the POIs of the route
				$modelPOIs = &$this->getModel( 'poi', 'ModelSocialRoutes' );
				//Set the POIs on the route
				$modelRoute->getRoute()->setPois($modelPOIs->getRoutePOIs($id));

				//Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = 'route';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
	
				//Set the model in the view
				if (!JError::isError( $modelRoute ) && !JError::isError($modelPOIs)) {
					$view->setModel($modelRoute, true );
					$view->setModel($modelPOIs);
				}
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}	
		}

		
		
		function saveRoute(){
			echo "saveroute";
			$option=JRequest::getCmd('option');
			
			$user =& JFactory::getUser();
			if($user->id)
			{
			    //Save the route in the request
				$model = &$this->getModel( 'route', 'ModelSocialRoutes' );
				
		        $join=JRequest::getVar('join', 0);
				
				//si se quieren unir 2 rutas existentes
				if ($join == 0){
				$routeID=$model->saveRouteFromRequest();
				}
				elseif ($join == 1) {
				$routeID=$model->saveRouteFromRequestJoin();
				//En caso de que no se halla encontrado intersección
				if ($routeID == 0){
					
					$this->setRedirect(JRoute::_('index.php?option=' . $option .'&task=searchForm&view=searchform'));
				}
				
				}
				
				$this->setRedirect(JRoute::_('index.php?option=' . $option .'&task=showRoute&id='.$routeID));
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function searchForm(){
			$user =& JFactory::getUser();
			if($user->id)
			{
			    //Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = 'searchForm';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function searchFormJoin(){
			$user =& JFactory::getUser();
			if($user->id)
			{
			    //Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = 'searchFormJoin';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function searchRoutes(){
			$user =& JFactory::getUser();
			if($user->id)
			{
			   	//Save the route in the request
				$model = &$this->getModel( 'routeList', 'ModelSocialRoutes' );
				$modelRoute = &$this->getModel( 'route', 'ModelSocialRoutes' );
				
				
				$model->searchRoutes();
				
				//Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = 'routeList';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the model in the view
				if (!JError::isError( $model )) {
					$view->setModel( $model, true );
					$view->setModel($modelRoute);
				}
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function showUserRoutes(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				//Save the route in the request
				$model = &$this->getModel( 'routeList', 'ModelSocialRoutes' );
				$modelRoute = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$model->getUserRoutes();
				
				//Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = 'routeList';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the model in the view
				if (!JError::isError( $model )) {
					$view->setModel( $model, true );
					$view->setModel($modelRoute);
				}
				
				//Set the layout template and call the view
				$view->setLayout('user');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function addUserRoute(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				//Save the route in the request
				$model = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$ok=$model->addUserRoute();
				
				echo JRequest::getVar('id',0);
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function removeUserRoute(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				//Save the route in the request
				$model = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$ok=$model->removeUserRoute();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function addRouteComment(){
			$option=JRequest::getCmd('option');
			
			$user =& JFactory::getUser();
			if($user->id)
			{
				//Read the route in the request
				$model = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$ok=$model->addRouteComment();
				
				$this->setRedirect(JRoute::_('index.php?option=' . $option .'&task=showRoute&id='.JRequest::getVar('routeID')));
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function addRoutePOI(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				$model = &$this->getModel( 'poi', 'ModelSocialRoutes' );
				$result=$model->addPOI();
				
				echo $result;
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function editRoutePOI(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				$model = &$this->getModel( 'poi', 'ModelSocialRoutes' );
				$result=$model->editPOI();
				
				echo $result;
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function deleteRoutePOI(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				$model = &$this->getModel( 'poi', 'ModelSocialRoutes' );
				$result=$model->deletePOI();
				
				echo $result;
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function downloadRoute(){
			$user =& JFactory::getUser();
			if($user->id)
			{
				$model = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$model->readRoute(JRequest::getVar('id',0));
				$model->downloadRoute();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		
		function similarRoute(){
			
			$user =& JFactory::getUser();
			if($user->id)
			{
				//Save the route in the request
				$model = &$this->getModel( 'routeList', 'ModelSocialRoutes' );
				$modelRoute = &$this->getModel( 'route', 'ModelSocialRoutes' );
				
				//Procedemos a la logica
				$id=JRequest::getVar('id', 0);
				$model->readRoute($id);
				$model->similar();
				
				//Get the document
				$document =&JFactory::getDocument();
			
				
				//Create the View
				$viewName = 'routeList';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the model in the view
				if (!JError::isError( $model )) {
					$view->setModel( $model, true );
					$view->setModel($modelRoute);
				}
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
			
				
		}
		
		
		function uploadForm(){
			$user =& JFactory::getUser();
			if($user->id)
			{
			    //Get the document
				$document =&JFactory::getDocument();
				
				//Create the View
				$viewName = 'uploadForm';
				$viewType = $document->getType();
				$view = &$this->getView($viewName, $viewType);
				
				//Set the layout template and call the view
				$view->setLayout('default');
				$view->display();
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
		
		function uploadRoute(){
			$option=JRequest::getCmd('option');
			
			$user =& JFactory::getUser();
			if($user->id)
			{
				//Read the route from file
				$model = &$this->getModel( 'route', 'ModelSocialRoutes' );
				$routeID=$model->saveRouteFromUpload();
	
				$this->setRedirect(JRoute::_('index.php?option=' . $option .'&task=showRoute&id='.$routeID));
			}
			else{
				$this->setRedirect(JRoute::_('index.php?option=com_community'));
			}
		}
	}
?>